package com.isunland.managesystem.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.PayMoneyLog;
import com.isunland.managesystem.entity.PayMoneyLogDetailOriginal;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import com.isunland.managesystem.widget.SingleLineView;
import org.json.JSONException;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/21 18:35
 */
public class PayMoneyLogDetailFragment extends BaseFragment implements View.OnClickListener {
  private static final int MENU_ITEM_BACK_MONEY = 1;
  public static final int FORM = 2;
  @BindView(R.id.sl_orderNo) SingleLineView slBookOrderNo;
  @BindView(R.id.sl_payWay1) SingleLineView slPayWay1;
  @BindView(R.id.sl_payTime) SingleLineView slPayTime;
  @BindView(R.id.sl_payMuch) SingleLineView slPayMuch;
  @BindView(R.id.sl_payAccount) SingleLineView slPayAccount;
  @BindView(R.id.sl_receiveAccount) SingleLineView slReceiveAccount;
  @BindView(R.id.sl_moneyType) SingleLineView slMoneyType;
  @BindView(R.id.sl_operationWayName) SingleLineView slOperationWayName;
  @BindView(R.id.sl_ifReturn) SingleLineView slIfReturn;
  @BindView(R.id.sl_returnId) SingleLineView slReturnId;
  @BindView(R.id.sl_regStaffName1) SingleLineView slRegStaffName1;
  @BindView(R.id.sl_regDate1) SingleLineView slRegDate1;
  @BindView(R.id.include1) View include1;
  @BindView(R.id.include2) View include2;
  private PayMoneyLog mItem;

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_pay_money_log_detail, container, false);
    ButterKnife.bind(this, view);
    return view;
  }

  @Override public void initData() {
    super.initData();
    if (mBaseParams.getItem() == null) return;
    if (!(mBaseParams.getItem() instanceof PayMoneyLog)) return;
    mItem = (PayMoneyLog) mBaseParams.getItem();
    if (mItem == null) mItem = new PayMoneyLog();
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    if (MyStringUtil.equals(mItem.getOrderType(), PayMoneyLog.TYPE_BACK)) {
      setTitleCustom(R.string.backDetail);
    } else {
      setTitleCustom(R.string.payDetail);
    }
    bindData(mItem);
    volleyPostInit();
  }

  private void volleyPostInit() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_QUERY_DETAIL_PAY_MONEY_LOG);
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("id", mItem.getId());
    mActivity.volleyPost(url, params.getParams(), new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {

      }

      @Override public void onVolleyResponse(String json) throws JSONException {
        PayMoneyLogDetailOriginal result =
            new Gson().fromJson(json, PayMoneyLogDetailOriginal.class);
        mItem = result.getData();
        bindData(mItem);
      }
    });
  }

  private void bindData(PayMoneyLog mItem) {
    if (mItem == null) return;
    //有退款id,并且不是退款详情时可以显示退款详情按钮
    if ((!TextUtils.isEmpty(mItem.getReturnId())) && (!MyStringUtil.equals(mItem.getOrderType(),
        PayMoneyLog.TYPE_BACK))) {
      slReturnId.setVisibility(View.VISIBLE);
    }
    if (MyStringUtil.equals(mItem.getOrderType(), PayMoneyLog.TYPE_WORK)) {
      include1.setVisibility(View.GONE);
      include2.setVisibility(View.GONE);
      slPayAccount.setVisibility(View.GONE);
      slReceiveAccount.setVisibility(View.GONE);
      slMoneyType.setVisibility(View.GONE);
      slOperationWayName.setVisibility(View.GONE);
      slIfReturn.setVisibility(View.GONE);
      slReturnId.setVisibility(View.GONE);
    }
    if (MyStringUtil.equals(mItem.getOrderType(), PayMoneyLog.TYPE_BACK)) {
      slIfReturn.setVisibility(View.GONE);
      slReturnId.setVisibility(View.GONE);
    }
    slReturnId.setHint("");
    slBookOrderNo.setHint("");
    slReturnId.setOnClickContentListener(this);
    slBookOrderNo.setOnClickContentListener(this);

    slBookOrderNo.setTextContent(mItem.getBookOrderNo());
    slIfReturn.setTextContent(
        TextUtils.isEmpty(mItem.getIfReturn()) ? getString(R.string.no) : mItem.getIfReturn());
    slMoneyType.setTextContent(mItem.getMoneyType());
    slOperationWayName.setTextContent(mItem.getOperationWay());
    slPayWay1.setTextContent(mItem.getPayWay());
    slPayAccount.setTextContent(mItem.getPayAccount());
    slPayMuch.setTextContent(mItem.getPayMuch());
    slPayTime.setTextContent(mItem.getPayTime());
    slReceiveAccount.setTextContent(mItem.getReceiveAccount());
    slRegDate1.setTextContent(mItem.getRegDate());
    slRegStaffName1.setTextContent(mItem.getRegStaffName());
  }

  @Override public void onClick(View v) {
    if (!(v.getTag() instanceof Integer)) return;
    int id = (int) v.getTag();
    switch (id) {
      case R.id.sl_returnId:
        if (TextUtils.isEmpty(mItem.getReturnId())) return;
        BaseParams<PayMoneyLog> params = new BaseParams<>();
        PayMoneyLog item = new PayMoneyLog();
        item.setId(mItem.getReturnId());
        item.setOrderType(PayMoneyLog.TYPE_BACK);
        params.setItem(item);
        PayMoneyLogDetailActivity.newInstance(this, PayMoneyLogDetailActivity.class, params, 0);
        break;
      case R.id.sl_orderNo:
        jumpById(mItem);
        break;
      default:
        break;
    }
  }

  private void jumpById(PayMoneyLog mItem) {
    if (mItem == null || MyStringUtil.isEmpty(mItem.getOrderType())) return;
    BaseParams params = new BaseParams();
    params.setId(mItem.getMainId());
    params.setFrom(FORM);
    switch (mItem.getOrderType()) {
      case PayMoneyLog.TYPE_WORK:
        LocaleWorkQueryCountDetailActivity.newInstance(this,
            LocaleWorkQueryCountDetailActivity.class, params, 0);
        break;
      case PayMoneyLog.TYPE_LEASE:
        LeaseOrderDetailActivity.newInstance(this, LeaseOrderDetailActivity.class, params, 0);
        break;
      case PayMoneyLog.TYPE_ORDER:
        AppointmentOrderDetailActivity.newInstance(this, AppointmentOrderDetailActivity.class,
            params, 0);
        break;

      default:
        break;
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if (MyStringUtil.equals(mItem.getOrderType(), PayMoneyLog.TYPE_LEASE) || MyStringUtil.equals(
        mItem.getOrderType(), PayMoneyLog.TYPE_ORDER)) {
      menu.add(0, MENU_ITEM_BACK_MONEY, 0, getString(R.string.backMoney));
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case MENU_ITEM_BACK_MONEY:
        volleyPostBack();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  private void volleyPostBack() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_UPDATE_BACK_MONEY);
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("id", mItem.getId());
    params.put("mainId", mItem.getMainId());
    params.put("tab", mItem.getTab());
    mActivity.volleyPost(url, params.getParams(), new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {
        ToastUtil.show(R.string.failure_operation);
      }

      @Override public void onVolleyResponse(String json) throws JSONException {
        Base result = new Gson().fromJson(json, Base.class);
        ToastUtil.show(result.getMessage());
        if (result.getResult() == Base.SUCCESS) {
          mActivity.setResult(Activity.RESULT_OK);
          mActivity.finish();
        }
      }
    });
  }
}
